package com.example.fitapp.activity.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.http.util.OkHttpUtil;
import com.example.fitapp.activity.http.util.ApiListener;
import com.example.fitapp.activity.http.util.ApiUtil;
import com.example.fitapp.activity.http.api.user.UserRegistered;

public class RegisteredActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText email;
    private EditText nickname;
    private EditText password1;
    private EditText password2;
    private String emailText ;
    private String nicknameText ;
    private String password1Text ;
    private String password2Text ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        Button button = (Button) findViewById(R.id.button_register);
        email = (EditText) findViewById(R.id.edit_email);
        nickname = (EditText) findViewById(R.id.edit_nickname);
        password1 = (EditText) findViewById(R.id.edit_password1);
        password2 = (EditText) findViewById(R.id.edit_password2);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.button_register:
                 emailText = email.getText().toString();
                 nicknameText = nickname.getText().toString();
                 password1Text = password1.getText().toString();
                 password2Text = password2.getText().toString();
                if(emailText.length()<1){
                    Toast.makeText(RegisteredActivity.this,"请输入邮箱",Toast.LENGTH_SHORT).show();
                }
                sendRegistered();
                break;
            default:
                break;
        }
    }

    private void sendRegistered(){
        OkHttpUtil.init();
        new UserRegistered(emailText,password1Text,password2Text,nicknameText).post(new ApiListener() {
            @Override
            public void success(ApiUtil apiUtil) {
                UserRegistered api = (UserRegistered)apiUtil;
                String response = api.mResponse;
                System.out.println(response);
                Toast.makeText(MyApplication.getContext(),"注册成功",Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void failure(ApiUtil apiUtil) {

            }
        });

    }
}
